package br.com.seosoft.simplecalcfinan;

import java.util.Date;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.google.analytics.tracking.android.EasyTracker;

public class ActivyGeral extends SherlockFragmentActivity {
	/**
	 * @see android. app.Activity#onCreate(Bundle)
	 */
	public SharedPreferences preference;
	public SharedPreferences.Editor editor;
	Boolean autenticacao = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// getSherlock().getActionBar().setHomeButtonEnabled(true);

		if (!this.getClass().equals(inicial.class)) {
			getSupportActionBar().setHomeButtonEnabled(true);
			getSupportActionBar().setDisplayHomeAsUpEnabled(true);
		}

		// // Veririfica versao
		// if (VERSION.SDK_INT >= 14) {
		//
		// setTheme(android.R.style.Theme_Holo_Light);
		//
		// }
		// ANIMACAO DAS TELAS
		overridePendingTransition(android.R.anim.slide_in_left,
				android.R.anim.slide_out_right);

		super.onCreate(savedInstanceState);

		preference = PreferenceManager
				.getDefaultSharedPreferences(getApplicationContext());

		autenticacao = preference.getBoolean("senha", false);

	}


	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub

		switch (item.getItemId()) {

		case R.id.menuRemoverAds:
			Intent it = new Intent(this, ActComprarItens.class);
			startActivity(it);
			return true;

		case R.id.menuImportarSms:

			Intent it2 = new Intent(this, LerSmsAntigos.class);
			startActivity(it2);
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}

	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();

		// VERIFICA INTERVALO PRA PEDIR SENHA
		EasyTracker.getInstance().activityStart(this); // Analytics
		int intervaloTempo = 0;
		Date data = new Date(System.currentTimeMillis());

		if (geral.intervaloInteracao != null) {

			intervaloTempo = geral.intervaloInteracao.compareTo(data);
			Log.i(geral.LogCat, "TEMPO INTERVALO: " + intervaloTempo);
			Log.i(geral.LogCat, "TEMPO onStart: " + data.getTime());

			if (intervaloTempo == -1) {

				geral.logado = false;

			}
		}

		Log.v(geral.LogCat, "autenticacao : " + autenticacao);

		if (autenticacao == true && geral.logado == false) {

			Intent it = getIntent();

			Intent solicitarSenha = new Intent(getApplicationContext(),
					SolicitarSenha.class);

			solicitarSenha.replaceExtras(it);
			solicitarSenha.setData(it.getData());
			solicitarSenha.putExtra("class", getClass().getName());

			startActivity(solicitarSenha);

			finish();

			Log.v(geral.LogCat, "nome da class passando : "
					+ getClass().getName());

		}

	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub

		if (getClass().getName().equals(inicial.class.getName())) {

			geral.logado = false;
			// finish();
			finish();
		}

		super.onBackPressed();
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub

		super.onPause();

	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		EasyTracker.getInstance().activityStop(this); // Analytics
	}

	@Override
	public void onUserInteraction() {
		// TODO Auto-generated method stub

		Date data = new Date(System.currentTimeMillis()
				+ preference.getInt("tempo", 30000));

		geral.intervaloInteracao = data;
		// Log.i(geral.LogCat, ">>>INTERA����O<<<");
		// Log.i(geral.LogCat, "TEMPO: " + geral.intervaloInteracao.getTime());
		// Log.i(geral.LogCat,
		// "TEMPO SETADO COM SENHA: " + preference.getInt("tempo", 30000));

		super.onUserInteraction();
	}

}
